<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use app\common\model\AppAccountLog;
use app\common\model\BonusLog;
use app\common\model\CoinAddress;
use app\common\model\CompanyAccount;
use app\common\model\CompanyAccountLog;
use app\common\model\UserAccountLog;
use app\common\model\UserRankingData;
use think\Db;

/**
 * Recharge逻辑
 */
class Recharge extends LogicBase
{

    /**
     * 获取线下充值Recharge列表
     */
    public function getShareRechargeList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }





        $where['a.status'] = 1;


        $list = $this->modelCompanyAccountLog->alias('a')->field($field)->where($where)->field($field)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count=$this->modelCompanyAccountLog->alias('a')->where($where)->count();





        return ['count'=>$count,'list'=>$list,'ke'=>CompanyAccount::get(2)];



    }
    /**
     * 获取应用余额转账Recharge列表
     */
    public function getAppTransferList($data = [], $field = 'a.*,c.username ', $order = 'state asc,id desc', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }


        !empty($data['search_data']) && $where['c.username|c.mobile']=$data['search_data'];



        $where['a.status'] = 1;


        $list = $this->modelAppBalanceWithdrawal->alias('a')->field($field)->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->field($field)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count=$this->modelAppBalanceWithdrawal->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();





        return ['count'=>$count,'list'=>$list];



    }

    /**
     * 共生余额充值
     */
    public function  share_recharge($data){
        $url=url('share_balance_recharge_log');
        if(empty($data['balance'])){
            return [RESULT_ERROR, '金额必须填'];
        }
        if(empty($data['remark'])){
            return [RESULT_ERROR, '备注必须填'];
        }
        CompanyAccount::where(['id'=>2])->setInc('balance',$data['balance']);
        //写入充值记录
        CompanyAccountLog::create([
            'number'=>$data['balance'],
            'remark'=>$data['remark'],
            'admin_name'=>session('member_info.username')
        ]);

        return  [RESULT_SUCCESS, '操作成功', $url] ;

    }
    /**
     * 应用余额充值审核
     */
    public function  app_transfer_check($data){

        $info=$this->modelAppBalanceWithdrawal->getInfo(['id'=>$data['id']]);
        $data['admin_name']=session('member_info.username');
        if($data['state']==2){
            //审核不通过
            //增加应用余额
            \app\common\model\Member::where(['id'=>$info['user_id']])->setInc('app_balance',$info['money']);
            $user_account = [
                'user_id' => $info['user_id'],
                'type' => 5,
                'king_id' => 0,
                'is_add' => 2,
                'remark'=>'线下支付宝扫码支付消费失败解冻',
                'create_time' => TIME_NOW,
                'number' => $info['number']
            ];
            AppAccountLog::create($user_account);
            BonusLog::create([
                'user_id' =>  $info['user_id'],
                'type' => 4,
                'number'=>$info['number'],
                'remark'=>'线下支付宝扫码支付消费失败退回'
            ]);
            $this->modelAppBalanceWithdrawal->setInfo($data);

        }elseif($data['state']==1){
            $url=getConfig('alipay_url');
            $key=getConfig('alipay_aplkey');
            $arr=[
                'apikey'=>$key,
                'payee_account'=>$info['alipay_name'],
                'amount'=>$info['money'],
                'payer_show_name'=>getConfig('web_base_name')
            ];

            $res=mycurl($url,$arr,1);
            $res=json_decode($res,true);
            if($res['code']==200){

                if($res['data'][0]['code']==200){
                    $data['trade_no']=$res['data'][0]['out_biz_no'];
                    $data['state']=1;
                    $this->modelAppBalanceWithdrawal->setInfo($data);
                }else{
                    return  [RESULT_ERROR, $res['data'][0]['error']] ;
                }
            }else{
                return  [RESULT_ERROR, $res['msg']] ;
            }

        }


        return  [RESULT_SUCCESS, '操作成功'] ;

    }
    /**
     * 获取线下充值Recharge列表
     */
    public function getRechargeList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
             $where1['c.username'] =$data['search_data'] ;
        }
        if($data['paytype']==1){
            if(!empty($data['legal'])){
                $where['a.legal']=$data['legal'];
            }
        }else{
            if(!empty($data['form_wallet_id'])){
                $where['a.form_wallet_id']=$data['form_wallet_id'];
            }
        }



       if ( isset($data['state'])) {
           if (  !empty($data['state']) || $data['state']=='0') {
               $where['a.state'] = $data['state'];
           }
        }



        $where['paytype']=$data['paytype'];
        $where['a.status'] = 1;
        $where['a.type'] = 4;
        if (!empty($where1)) {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);

            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();

        } else {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->field($field)->order($order)->paginate($paginate, false, ['query' => $data]);
            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        }


        if($data['paytype']==1){
            $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');

            $btc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('money');

            $ke=$ke?$ke:0;
            $btc=$btc?$btc:0;

            return ['count'=>$count,'list'=>$list,'ke'=>$ke,'btc'=>$btc];
        }else{
            $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('money');
            $where['a.form_wallet_id']=6;
            $eth=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
            $where['a.form_wallet_id']=7;
            $btc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
            $where['a.form_wallet_id']=8;
            $usdt=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');


            return ['count'=>$count,'list'=>$list,'ke'=>$ke+0,'btc'=>$btc+0,'eth'=>$eth+0,'usdt'=>$usdt+0];
        }



    }


    /**
     * 导出线下充值列表
     */
    public function exportRechargeList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }
        if($data['paytype']==1){
            $where['a.legal']=$data['legal'];
            if(!empty($data['legal'])){
                $where['a.legal']=$data['legal'];
            }
        }else{
            if(!empty($data['form_wallet_id'])){
                $where['a.form_wallet_id']=$data['form_wallet_id'];
            }
        }



        if ( isset($data['state'])) {
            if (  !empty($data['state']) || $data['state']=='0') {
                $where['a.state'] = $data['state'];
            }
        }


        $where['a.form_wallet_id']=0;

        $where['paytype']=$data['paytype'];
        $where['a.status'] = 1;
        $where['a.type'] = 4;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k=>$v){
            $list[$k]['k']=$k+1;
            if($data['paytype']==1){
                $list[$k]['pay_name']=$v->legal==1?'支付宝':'微信';
            }else{
                $list[$k]['wallet_name']=$v->wallet->data['name'];
            }

            $list[$k]['state_text']=$v->state_text;
            if($v['state']==0){
                $list[$k]['up_time']='------';
            }else{
                $list[$k]['up_time']=date('Y-m-d H:i:s',$v->update_time);
            }
        }
        if($data['paytype']==1){
            $titles = "序号,用户名,用户ID,支付类型,充值金额,到账金额,充值状态,充值时间,审核时间,审核人";
            $keys = "k,username,user_id,pay_name,money,number,state_text,create_time,up_time,admin_name";

            action_log('导出', '导出线下法币充值列表');

            export_excel($titles, $keys, $list, '线下法币充值列表');
        }else{
            $titles = "序号,用户名,用户ID,币种,充值金额,到账金额,充值状态,充值时间,审核时间,审核人";
            $keys = "k,username,user_id,wallet_name,money,number,state_text,create_time,up_time,admin_name";

            action_log('导出', '导出线下钱包充值列表');

            export_excel($titles, $keys, $list, '线下钱包充值列表');
        }
//        dd(json_decode(json_encode($list),true));

    }





    /**


    /**
     * 充值审核
     */
    public function recharge_check($data=[])
    {

        $info = $this->modelTransfer->getInfo(['id' => $data['id']]);
        Db::startTrans();
        try {


            if ($data['state'] == 1) {
                //审核通过
                if($data['recharge_type']==1){
                    //线下充值
                    if($info->member->mobile){
                        //发消息
                        send_sms($info->member->mobile,'您通过线下充值,已到账,请注意查收' ,config('settings'),$info->member->area);

                    }
                    //账户变动
                    $number = $info['money'];
                    $useracount = $this->modelUserAccount->where(['wallet_id' => 1, 'user_id' => $info['user_id']])->lock(true)->find();
                    $useracount->balance +=$number;
                    $useracount->save();
                    //写入流水
                    UserAccountLog::create([
                        'user_id' => $info['user_id'],
                        'number' => $number,
                        'wallet_id' => 1,
                        'before_blance' => $useracount->balance - $number,
                        'after_blance' => $useracount->balance,
                        'remark' =>'线下充值',
                        'is_add' => 1,
                        'type' => 2
                    ]);
                    $info->member->daily_recharge+=$info['money'];
                    $info->member->week_recharge+=$info['money'];
                    $info->member->month_recharge+=$info['money'];
                    $info->member->recharge+=$info['money'];
                    $info->member->save();
                }
                if($data['recharge_type']==2){
                    //积分钱包充值充值
                    //账户变动
                    $number = $info['money'];
                    $useracount = $this->modelUserAccount->where(['wallet_id' => 1, 'user_id' => $info['user_id']])->lock(true)->find();

                    $useracount->balance +=$number;
                    $useracount->save();
                    //写入流水
                    UserAccountLog::create([
                        'user_id' => $info['user_id'],
                        'number' => $number,
                        'wallet_id' => 1,
                        'before_blance' => $useracount->balance - $number,
                        'after_blance' => $useracount->balance,
                        'remark' =>'充值总包',
                        'is_add' => 1,
                        'type' => 14
                    ]);



                    $eth_account = $this->modelUserAccount->where(['wallet_id' => $info['form_wallet_id'], 'user_id' => $info['user_id']])->lock(true)->find();
                    $eth_account->freeze_balance -=$info['number'];
                    $eth_account->save();
                    //写入流水
                    UserAccountLog::create([
                        'user_id' => $info['user_id'],
                        'number' => $number,
                        'wallet_id' => $info['form_wallet_id'],
                        'before_blance' => $eth_account->balance+$number,
                        'after_blance' => $eth_account->balance,
                        'remark' =>'充值总包',
                        'is_add' => 2,
                        'type' => 14
                    ]);
                    $info->member->daily_recharge+=$info['money'];
                    $info->member->week_recharge+=$info['money'];
                    $info->member->month_recharge+=$info['money'];
                    $info->member->recharge+=$info['money'];
                    $info->member->save();

                }



            }else{
                if($data['recharge_type']==2){
                    //积分钱包充值充值
                    $eth_account = $this->modelUserAccount->where(['wallet_id' => $info['form_wallet_id'], 'user_id' => $info['user_id']])->lock(true)->find();
                    $eth_account->freeze_balance -=$info['number'];
                    $eth_account->balance +=$info['number'];
                    $eth_account->save();
                }
            }
            //写入排行榜记录
            UserRankingData::create([
                'user_id'=>$info['user_id'],
                'type'=>1,
                'is_user'=>1,
                'money'=>$info['number'],
                'remark'=>'总包线下充值'
            ]);

            $data['admin_name']=session('member_info.username');
            $result = $this->modelTransfer->setInfo($data);
            $msg = $data['state'] == 1 ? '审核成功' : '拒绝审核';
            $result && action_log('充值审核', $msg);
            Db::commit();
            return $result ? [RESULT_SUCCESS, '审核成功'] : [RESULT_ERROR, '审核失败'];

        } catch (\Exception $e) {
            Db::rollback();
            return [RESULT_ERROR, $e->getMessage()];
        }
    }


    /**
     * 提币审核
     */
    public function withdrawal_check($data=[])
    {

        $info = $this->modelTransfer->getInfo(['id' => $data['id']]);
        $url = COIN_URL."/api/v1/transfer/add";
        Db::startTrans();
        try {
            //区块链充值

            if ($data['state'] == 3) {

                $number = $info['number']-$info['poundage_number'];

                switch ($info['form_wallet_id']){
                    case 6:

                        $post_data = array(
                            "code" => "ETH",
                           "from_address" => $info->form,
                           "to_address" => $info->to,
                           "number" => $number,
                            'password'=>$info->wallet['wallet_password'],
                           "nonce_str" => rand(1000,9999)
                      );
                        break;
                    case 7:
                        $post_data = array(
                            "code" => "BTC",
                            "from_address" => $info->form,
                            "to_address" => $info->to,
                            "number" => $number,
                            'password'=>$info->wallet['wallet_password'],
                            "nonce_str" => rand(1000,9999)
                        );
                        break;
                    case 8:
                        $post_data = array(
                            "code" => "ETH/USDT",
                            "from_address" => $info->form,
                            "to_address" => $info->to,
                            "number" => $number,
                            'password'=>$info->wallet['wallet_password'],
                            "nonce_str" => rand(1000,9999),

                        );
                        break;
                }

                $post_data['contractaddress'] = $info->wallet['contractaddress'];
                $post_data['private_key'] = '';
                $post_data['gas_limit'] = '';
                $post_data['gas_price'] = '';

                $sign = md5(asc_sort($post_data).'&key='."79ba9e912e708a0a3177df0641be1e5a");
                $post_data['sign'] = $sign;
                
                $dat=json_decode(mycurl($url,$post_data,1),true);
                
                 if($dat['status']==1){
                     $request_arr=$dat['data'];
               // 改变状态，写入交易哈希

                     $data['hash_tx']=$request_arr['txId'];
                     $data['hash']=$request_arr['txId'];
                     $data['txFee']=$request_arr['fee'];
                  }else{
                     return [RESULT_ERROR,$dat['msg']];
                   }
            }else{
                $eth_account = $this->modelUserAccount->where(['wallet_id' => $info['form_wallet_id'], 'user_id' => $info['user_id']])->lock(true)->find();
                $eth_account->freeze_balance -=$info['number'];
                $eth_account->balance +=$info['number'];
                $eth_account->save();
            }
            $data['admin_name']=session('member_info.username');
            $result = $this->modelTransfer->setInfo($data);
            $msg = $data['state'] == 1 ? '审核成功' : '拒绝审核';
            $result && action_log('提币审核', $msg);
            Db::commit();
            return $result ? [RESULT_SUCCESS, '审核成功'] : [RESULT_ERROR, '审核失败'];

        } catch (\Exception $e) {
            Db::rollback();
            return [RESULT_ERROR, $e->getMessage()];
        }
    }




    /**
     * 获取划转记录列表
     */
    public function geEthTransferList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }

        if(!empty($data['wallet_id'])){
            $where['a.form_wallet_id']=$data['wallet_id'];
        }

        if ( isset($data['state'])) {
            if (  !empty($data['state']) || $data['state']=='0') {
                $where['a.state'] = $data['state'];
            }
        }

        $where['a.status'] = 1;
        $where['a.type'] = ['in',[16,17]];
        if (!empty($where1)) {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);

            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();

        } else {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->field($field)->order($order)->paginate($paginate, false, ['query' => $data]);
            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        }
        $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('money');
        $where['a.form_wallet_id']=6;
        $eth=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['a.form_wallet_id']=7;
        $btc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['a.form_wallet_id']=8;
        $usdt=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');


        return ['count'=>$count,'list'=>$list,'ke'=>$ke+0,'btc'=>$btc+0,'eth'=>$eth+0,'usdt'=>$usdt+0];




    }






    /**
     * 导出划转记录列表
     */
    public function exporEthTransferList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }

        if(!empty($data['wallet_id'])){
            $where['a.form_wallet_id']=$data['wallet_id'];
        }

        if ( isset($data['state'])) {
            if (  !empty($data['state']) || $data['state']=='0') {
                $where['a.state'] = $data['state'];
            }
        }

        $where['a.status'] = 1;
        $where['a.type'] = ['in',[16,17]];
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k=>$v){
            $list[$k]['k']=$k+1;
            $list[$k]['wallet_name']=$v->wallet->data['name'];
            $list[$k]['type_text']=$v->type_text;
        }

        $titles = "序号,用户名,用户ID,币种,类型,划转金额,划转时间";
        $keys = "k,username,user_id,wallet_name,type_text,number,create_time";

        action_log('导出', '导出划转记录列表');

        export_excel($titles, $keys, $list, '划转记录列表');

//        dd(json_decode(json_encode($list),true));

    }







    /**
     * 获取提币记录列表
     */
    public function withdrawal_log($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }

        if(!empty($data['form_wallet_id'])){
            $where['a.form_wallet_id']=$data['form_wallet_id'];
        }

        if ( isset($data['state'])) {
            if (  !empty($data['state']) || $data['state']=='0') {
                $where['a.state'] = $data['state'];
            }
        }

        if(!empty($data['type'])){
            $where['a.type']=  $data['type'];
        }
        $where['a.status'] = 1;

        if (!empty($where1)) {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);

            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();

        } else {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->field($field)->order($order)->paginate($paginate, false, ['query' => $data]);
            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        }

        switch ($data['type']){
            case  20:
                $where['a.form_wallet_id']=6;
                $eth=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
                $where['a.form_wallet_id']=7;
                $btc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
                $where['a.form_wallet_id']=8;
                $usdt=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
                return ['count'=>$count,'list'=>$list,'btc'=>$btc+0,'ect'=>$eth+0,'usdt'=>$usdt+0];
                break;

            case  21:
                $usdt=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
                return ['count'=>$count,'list'=>$list,'usdt'=>$usdt+0];
                break;
        }






    }






    /**
     * 导出提币记录列表
     */
    public function exportWithdrawalletList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {


        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }

        if(!empty($data['form_wallet_id'])){
            $where['a.form_wallet_id']=$data['form_wallet_id'];
        }

        if ( isset($data['state'])) {
            if (  !empty($data['state']) || $data['state']=='0') {
                $where['a.state'] = $data['state'];
            }
        }
        if(!empty($data['type'])){
            $where['a.type']=  $data['type'];
        }
        $where['a.status'] = 1;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }


        switch ($data['type']){
            case  20:
                foreach ($list as $k=>$v){
                    $list[$k]['k']=$k+1;
                    $list[$k]['wallet_name']=$v->wallet->data['name'];
                    $list[$k]['true_number']=$v->number-$v->poundage_number;
                    $list[$k]['state_text']=$v->state_text;
                    if($v['state']==0){
                        $list[$k]['up_time']='------';
                    }else{
                        $list[$k]['up_time']=date('Y-m-d H:i:s',$v->update_time);
                    }
                }
                $titles = "序号,用户名,用户ID,币种,地址,交易哈希,提现金额,手续费,到账金额,状态,提现时间,审核时间,审核人";
                $keys = "k,username,user_id,wallet_name,to,hash,number,poundage_number,true_number,state_text,create_time,up_time,admin_name";

                action_log('导出', '导出提币记录列表');

                export_excel($titles, $keys, $list, '提币记录列表');
                break;

            case  21:
                foreach ($list as $k=>$v){
                    $list[$k]['k']=$k+1;
                }
                $titles = "序号,用户名,用户ID,金额(总包),充值时间,游戏订单号";
                $keys = "k,username,user_id,number,create_time,remark";

                action_log('导出', '导出总包充值游戏记录列表');

                export_excel($titles, $keys, $list, '总包充值游戏记录列表');
                break;
        }


//        dd(json_decode(json_encode($list),true));

    }























    /**
         * 转账记录列表
         */
        public function getTransferList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }


        if (!empty($data['search_data'])) {

            $where['c.username|a.user_id'] =$data['search_data'];
        }
        if (!empty($data['wallet_id'])) {

            $where['a.form_wallet_id'] =$data['wallet_id'];
        }
        if (!empty($data['user_name'])) {

            $where['d.username|a.uid'] =$data['user_name'];
        }
        $where['a.status'] =1;
        $where['a.type'] =1;


        $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->join('__MEMBER__ d' ,'a.uid = d.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count=   $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->join('__MEMBER__ d' ,'a.uid = d.id')->where($where)->count();
        $shan =   $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->join('__MEMBER__ d' ,'a.uid = d.id')->where($where)->sum('a.number');
        $shan = $shan ? $shan : 0;
        return ['count' => $count, 'list' => $list,  'shan' => $shan];


    }


    /**
     * 导出转账记录列表
     */
    public function exportTransferList($data, $field = 'm.*,b.nickname as leader_nickname', $order = 'id desc')
    {
        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }


        if (!empty($data['search_data'])) {

            $where['c.username|a.user_id'] =$data['search_data'];
        }
        if (!empty($data['user_name'])) {

            $where['d.username|a.uid'] =$data['user_name'];
        }
        if (!empty($data['wallet_id'])) {

            $where['a.form_wallet_id'] =$data['wallet_id'];
        }
        $where['a.status'] =1;
        $where['a.type'] =1;

        $list = $this->modelTransfer->alias('a')->field('a.*,c.username')->join('__MEMBER__ c' ,'a.user_id = c.id')->join('__MEMBER__ d' ,'a.uid = d.id')->where($where)->order($order)->select();



        foreach ($list as $k => $v) {
            $list[$k]['k'] = $k+1;
            $list[$k]['wallet_name'] = $v->wallet['name'];
            $list[$k]['usernickname'] = $v->usernickname;

        }

        $titles = "序号,ID,转出账号,转出金额,钱包类型,转入账户,转让时间";
        $keys = "k,id,username,number,wallet_name,usernickname,create_time";
        action_log('导出', '导出转账记录');
        export_excel($titles, $keys, $list, '转账记录');


    }


    /**
     * 后台充值列表
     */
    public function getAdminRechargeList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'];
        }
        if (!empty($data['wallet_id'])) {
            $where['a.form_wallet_id'] = $data['wallet_id'];
        }
        if (!empty($data['is_legal_currency'])) {
            $where['a.is_legal_currency'] = $data['is_legal_currency'];
        }
        if (isset($data['is_legal_currency']) && $data['is_legal_currency']='0' ) {
            $where['a.is_legal_currency'] = $data['is_legal_currency'];
        }
        $where['a.status'] =1;
        $where['a.type'] =12;
        if (!empty($where1)) {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);
            $count =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();

        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
            $count =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->count();
        }
        $where['form_wallet_id']=1;
        $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['form_wallet_id']=2;
        $shan=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['form_wallet_id']=3;
        $gc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $ke=$ke?$ke:0;
        $shan=$shan?$shan:0;
        $gc=$gc?$gc:0;
        $where['form_wallet_id']=4;
        $usdt=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['form_wallet_id']=5;
        $ev=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['form_wallet_id']=9;
        $cv=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $usdt=$usdt?$usdt:0;
        $ev=$ev?$ev:0;
        $cv=$cv?$cv:0;

        return ['count'=>$count,'list'=>$list,'ke'=>$ke,'shan'=>$shan,'gc'=>$gc,'usdt'=>$usdt,'ev'=>$ev,'cv'=>$cv];

    }



    /**
     * 导出手动充值记录列表
     */
    public function exportAdminRechargeList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        set_time_limit(0);


        $where = $where1=[] ;
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'];
        }
        if (!empty($data['wallet_id'])) {
            $where['a.form_wallet_id'] = $data['wallet_id'];
        }



        $where['a.status'] =1;
        $where['a.type']=12;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k=>$v){

            $list[$k]['k']=$k+1;
            $list[$k]['wallet_name']=$v->wallet->data['name'];

        }

//        dd(json_decode(json_encode($list),true));
        $titles = "序号,ID,用户账号,钱包类型,充值金额,充值备注,充值时间,充值人";
        $keys = "k,id,username,wallet_name,number,remark,create_time,admin_name";

        action_log('导出', '导出手动充值记录');

        export_excel($titles, $keys, $list, '手动充值记录');
    }




    /**
     * 交易记录列表
     */
    public function getTradeList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {

        $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field);

        $where = $where1 = [];
        if (!empty($data['date'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
             $where1['c.username'] =$data['search_data'] ;
        }

        if (!empty($data['wallet_id'])) {
            $where['a.to_wallet_id'] = $data['wallet_id'];
        }

        if (!empty($data['state']) ) {
            $where['a.state'] = $data['state'];
        }
        if ( isset($data['state']) && $data['state']=='0') {
            $where['a.state'] = $data['state'];
        }
        $where['a.status'] =1;
        $where['a.type'] =9;
        if (!empty($where1)) {

            $list = $this->modelTransfer->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);
        } else {

            $list = $this->modelTransfer->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
        }

        return $list;

    }


    /**
     * 兑换记录页面
     */
    public function exchange_log($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1=[] ;
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (isset($data['state'])) {
            if($data['state']=='0' || !empty($data['state'])){
                $where['a.state'] = $data['state'];
            }
        }

        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }
        if (!empty($data['to_wallet_id'])) {
            $where['a.to_wallet_id'] = $data['to_wallet_id'];
        }
        if (!empty($data['form_wallet_id'])) {
            $where['a.form_wallet_id'] = $data['form_wallet_id'];
        }

        $where['a.status'] =1;
        $where['a.type']=2;

        if (!empty($where1)) {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);
            $count =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();

        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);

            $count =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->count();
        }

        $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $gc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('poundage_number');
        $shan=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('money');
        $ke=$ke?$ke:0;
        $gc=$gc?$gc:0;
        $shan=$shan?$shan:0;

        return ['count'=>$count,'list'=>$list,'ke'=>$ke,'gc'=>$gc,'shan'=>$shan];

    }

    /**
     * 导出兑换记录列表
     */
    public function exportExchangeList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        set_time_limit(0);


        $where = $where1=[] ;
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }


        if (!empty($data['to_wallet_id'])) {
            $where['a.to_wallet_id'] = $data['to_wallet_id'];
        }
        if (!empty($data['form_wallet_id'])) {
            $where['a.form_wallet_id'] = $data['form_wallet_id'];
        }

        $where['a.status'] =1;
        $where['a.type']=2;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        if($data['to_wallet_id']==1 && $data['form_wallet_id']==4 ){
            //盈利包转回总钱包记录导出
            foreach ($list as $k=>$v){
                $list[$k]['wallet_name']=$v->wallet['name'];
                $list[$k]['remark1']='闪兑兑换'.$v->towallet['name'];
                if($v['state']==0){
                    $list[$k]['up_time']='------';
                }else{
                    $list[$k]['up_time']=date('Y-m-d H:i:s',$v->update_time);
                }
                $list[$k]['state_text']=$v->state_text;
                $list[$k]['k']=$k+1;
            }

//        dd(json_decode(json_encode($list),true));
            $titles = "序号,ID,用户账号,转换金额,到账金额,手续费,状态,提交时间,审核时间,审核人";
            $keys = "k,id,username,number,money,poundage_number,state_text,create_time,up_time,admin_name";

            action_log('导出', '导出盈利包转回总钱包记录');
            export_excel($titles, $keys, $list, '盈利包转回总钱包记录');
        }

    }




    /**
     * 兑换记录页面
     */
    public function all_exchange_log($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1=[] ;
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (isset($data['state'])) {
            if($data['state']=='0' || !empty($data['state'])){
                $where['a.state'] = $data['state'];
            }
        }

        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }

        if (!empty($data['exchange_type'])) {
            $where['a.exchange_type'] =$data['exchange_type'];

        }else{
            $where['a.exchange_type'] =['gt',0];
        }

        $where['a.status'] =1;
        $where['a.type']=2;

        if (!empty($where1)) {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);
            $count =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();

        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);

            $count =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->count();
        }

        $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $gc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('poundage_number');
        $shan=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('money');
        $ke=$ke?$ke:0;
        $gc=$gc?$gc:0;
        $shan=$shan?$shan:0;

        return ['count'=>$count,'list'=>$list,'ke'=>$ke,'gc'=>$gc,'shan'=>$shan];

    }

    /**
     * 导出兑换记录列表
     */
    public function exportAllExchangeList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        set_time_limit(0);


        $where = $where1=[] ;
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (isset($data['state'])) {
            if($data['state']=='0' || !empty($data['state'])){
                $where['a.state'] = $data['state'];
            }
        }

        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =$data['search_data'] ;
        }

        if (!empty($data['exchange_type'])) {
            $where['a.exchange_type'] =$data['exchange_type'];

        }else{
            $where['a.exchange_type'] =['gt',0];
        }

        $where['a.status'] =1;
        $where['a.type']=2;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }

        //盈利包转回总钱包记录导出
        foreach ($list as $k=>$v) {
            $list[$k]['k'] = $k+1;
            $list[$k]['exchange_type'] = $v->exchange_type;
        }

//        dd(json_decode(json_encode($list),true));
        $titles = "序号,ID,类型,用户账号,转换金额,到账金额,手续费,提交时间";
        $keys = "k,id,exchange_type,username,number,money,poundage_number,create_time";

        action_log('导出', '导出转换记录');
        export_excel($titles, $keys, $list, '转换记录');


    }


    /**
     * 交易审核
     */
    public function update_info($data=[])
    {

        $info = $this->modelTransfer->getInfo(['id' => $data['id']]);
        Db::startTrans();
        try {

            switch ($data['type']){
                case 1:
                    //审核盈利包转回总包

                    if ($data['state'] == 1) {
                        //审核通过
                        $useracount = $this->modelUserAccount->where(['wallet_id' => 1, 'user_id' => $info['user_id']])->lock(true)->find();
                        $useracount->balance+=$info['money'];
                        $useracount->save();

                        //写入流水
                        UserAccountLog::create([
                            'user_id' => $info['user_id'],
                            'number' => $info->money,
                            'wallet_id' => $info['to_wallet_id'],
                            'before_blance' => $useracount->balance - $info['money'],
                            'after_blance' => $useracount->balance,
                            'is_add' => 1,
                            'remark'=>'盈利包转入总包',
                            'type' => 16
                        ]);

                        //通知用户
                        \app\common\model\UserNotice::create([
                            'user_id' => $info['user_id'],
                            'title' => '盈利包转入总包通过审核',
                            'content' => '恭喜您,您的盈利包转入总包订单已通过审核,请前往总包查收，如有疑问,请联系客服',
                        ]);


                    }else{
                        //拒绝审核
                        $useracount = $this->modelUserAccount->where(['wallet_id' => 4, 'user_id' => $info['user_id']])->lock(true)->find();
                        $useracount->balance+=$info['number'];
                        $useracount->save();

                        //写入流水
                        UserAccountLog::create([
                            'user_id' => $info['user_id'],
                            'number' => $info['number'],
                            'wallet_id' => 4,
                            'before_blance' => $useracount->balance -$info['number'],
                            'after_blance' => $useracount->balance,
                            'is_add' => 1,
                            'remark'=>'盈利包转入总包',
                            'type' => 16
                        ]);
                        //通知用户
                        \app\common\model\UserNotice::create([
                            'user_id' => $info['user_id'],
                            'title' => '盈利包转入总包未能通过审核',
                            'content' => '很遗憾,您的盈利包转入总包订单未能通过审核',
                        ]);
                    }
                    break;
            }



            unset($data['type']);
            $data['admin_name']=session('member_info.username');
            $result = $this->modelTransfer->setInfo($data);
            $msg = $data['state'] == 1 ? '审核成功' : '拒绝审核';
            $result && action_log('充值审核', $msg);
            Db::commit();
            return $result ? [RESULT_SUCCESS, '审核成功'] : [RESULT_ERROR, '审核失败'];

        } catch (\Exception $e) {
            Db::rollback();
            return [RESULT_ERROR, $e->getMessage()];
        }
    }



    /**
     * 余额流水页面
     */
    public function account_log($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {

        $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field);

        $where = $where1 = $where2=[];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
             $where1['c.username'] =$data['search_data'] ;
        }
        if (!empty($data['type'])) {
            $where['a.type'] = $data['type'];
        }

        if (!empty($data['wallet_id'])) {
            $where['a.wallet_id'] = $data['wallet_id'];
        }
        if (!empty($data['is_add'])) {
            $where['a.is_add'] = $data['is_add'];
        }

        $where['a.status'] =1;

        if (!empty($where1)) {

                $list = $this->modelUserAccountLog->where($where)->where(function ($q)use($where1){
                    $q->whereOr($where1);
                })->order($order)->paginate($paginate, false, ['query' => $data]);

        } else {

                $list = $this->modelUserAccountLog->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);

        }
//        dd($list);
        return $list;

    }



    /**
     * 静态释放页面
     */
    public function account_log2($data = [], $field = 'a.*,c.username ', $order = 'id desc', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = $where2=[];
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where['c.username|a.user_id'] =$data['search_data'];
        }
        if (!empty($data['type'])) {
            $where['a.type'] = $data['type'];
        }else{
            $where['a.is_reward'] =1;
        }

//        if (!empty($data['wallet_id'])) {
//            $where['a.wallet_id'] = $data['wallet_id'];
//        }

        $where['a.status'] =1;

        $list = $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count=   $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        $shan =   $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('a.number');
        $ke =   $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('a.company_number');
        $shan = $shan ? $shan : 0;

        return ['count' => $count, 'list' => $list,  'shan' => $shan,'ke'=>$ke];


    }

    /**
     * 导出静态释放红利记录列表
     */
    public function exportAccountList2($data, $field = 'a.*,c.username', $order = 'id desc')
    {
        $where = $where1 = $where2=[];
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where['c.username|a.user_id'] =$data['search_data'];
        }
        if (!empty($data['type'])) {
            $where['a.type'] = $data['type'];
        }



        $where['a.status'] =1;

        $list = $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();



        foreach ($list as $k => $v) {
            $list[$k]['k'] = $k+1;


        }

        $titles = "序号,ID,用户账号,用户UID,配资金额,盈利金额,盈利百分比(%),服务费,用户分红百分比(%),释放金额（红利）,平台分红百分比(%),平台红利,,释放时间";
        $keys = "k,id,username,user_id,pei_money,profit,profit_ratio,service_fee,bonus,number,company_ratio,company_number,create_time";
        action_log('导出', '导出静态释放红利记录');
        export_excel($titles, $keys, $list, '静态释放红利记录');


    }




    /**
     * 提纯EC页面
     */
    public function account_log3($data = [], $field = 'a.*,c.username ', $order = 'create_time desc,id desc', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = $where2=[];
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where['c.username'] =$data['search_data'];
        }

        if (!empty($data['type'])) {
            $where['a.type'] =$data['type'] ;
        }else{
            $where['a.is_reward'] =1;
        }

       // dd($where);
        $where['a.status'] =1;

        $list = $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count=   $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        $shan =   $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('a.number');
        $shan = $shan ? $shan : 0;

        return ['count' => $count, 'list' => $list,  'shan' => $shan];


    }

    /**
     * 导出奖金记录
     */
    public function exportAccountList3($data, $field = 'a.*,c.username', $order = 'create_time desc, id desc')
    {
        $where = $where1 = $where2=[];
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where['c.username'] =$data['search_data'];
        }


        if (!empty($data['type'])) {
            $where['a.type'] =$data['type'] ;
        }else{
            $where['a.is_reward'] =1;
        }

        $where['a.status'] =1;


        $list = $this->modelUserAccountLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();



        foreach ($list as $k => $v) {
            $list[$k]['k'] = $k+1;
            $list[$k]['type_text'] = $v->type_text;

        }

        $titles = "序号,ID,用户账号,奖金类型,收益总额,备注说明,记录时间";
        $keys = "k,id,username,type_text,number,remark,create_time";
        action_log('导出', '导出奖金记录');
        export_excel($titles, $keys, $list, '奖金记录');


    }



    /**
     * 交易审核
     */
    public function trade_check($data=[])
    {

        $info = $this->modelTransfer->getInfo(['id' => $data['id']]);
        Db::startTrans();
        try {
            $useracount = $this->modelUserAccount->where(['wallet_id' => 2, 'user_id' => $info['user_id']])->lock(true)->find();

            if ($data['state'] == 1) {
                //审核通过

                //闪兑钱包

                $useracount->freeze_balance-=($info['number']+$info['poundage_number']);
                $useracount->save();


                //账户变动

                $useracount1 = $this->modelUserAccount->where(['wallet_id' => $info['to_wallet_id'], 'user_id' => $info['user_id']])->lock(true)->find();
                $useracount1->balance += $info->account_balance;
                $useracount1->save();
                $data['before_against_balance']=$useracount->balance+$info['number']+$info['poundage_number'];
                $data['after_against_balance']=$useracount->balance;

                //写入流水
                UserAccountLog::create([
                    'user_id' => $info['user_id'],
                    'number' => $info->account_balance,
                    'wallet_id' => $info['to_wallet_id'],
                    'before_blance' => $useracount1->balance - $info->account_balance,
                    'after_blance' => $useracount1->balance,
                    'is_add' => 1,
                    'type' => 9
                ]);


            }else{
                //拒绝审核
                $useracount->balance+=($info['number']+$info['poundage_number']);
                $useracount->freeze_balance-=($info['number']+$info['poundage_number']);
                $useracount->save();

                //写入流水
                UserAccountLog::create([
                    'user_id' => $info['user_id'],
                    'number' => $info['number']+$info['poundage_number'],
                    'wallet_id' => 2,
                    'before_blance' => $useracount->balance - $info['number']-$info['poundage_number'],
                    'after_blance' => $useracount->balance,
                    'is_add' => 1,
                    'type' => 9
                ]);

            }

            $result = $this->modelTransfer->setInfo($data);
            $msg = $data['state'] == 1 ? '审核成功' : '拒绝审核';
            $result && action_log('充值审核', $msg);
            Db::commit();
            return $result ? [RESULT_SUCCESS, '审核成功'] : [RESULT_ERROR, '审核失败'];

        } catch (\Exception $e) {
            Db::rollback();
            return [RESULT_ERROR, $e->getMessage()];
        }
    }





    /**
     * 释放记录
     */
    public function release_log($data,$field = 'a.*,c.username', $order = 'a.id asc',$paginate = DB_LIST_ROWS){



        $where =[];
        if (!empty($data['date']) && !empty($data['date1']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {

            $where['c.username'] =['like','%'.$data['search_data'].'%'] ;
        }

        $where['a.form_wallet_id'] = 3;
        $where['a.status'] = 1;
        $where['a.type'] = 11;
        $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count=  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('available_number');
        $gc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $ke=$ke?$ke:0;
        $gc=$gc?$gc:0;

        return ['count'=>$count,'list'=>$list,'ke'=>$ke,'gc'=>$gc];
    }


    /**
     * 导出释放列表
     */
    public function exportReleaseList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        set_time_limit(0);
       ;

        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {

            $where['c.username'] =['like','%'.$data['search_data'].'%'] ;
        }

        $where['a.form_wallet_id'] = 3;
        $where['a.status'] = 1;
        $where['a.type'] = 11;
        $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();
        foreach ($list as $k=>$v){
	$list[$k]['k']=$k+1;
            $list[$k]['add_time']=date('Y-m-d H:i:s',$v->create_time);

        }

//        dd(json_decode(json_encode($list),true));
        $titles = "序号,释放ID,用户账号,充值金额（RC）,释放GC额,释放时间";
        $keys = "k,id,username,available_number,number,add_time";

        action_log('导出', '导出释放列表');

        export_excel($titles, $keys, $list, '释放列表');
    }



    /**
     * 购买课程记录页面
     */
    public function course_log($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1=[] ;
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {

            $where['c.username'] =$data['search_data'] ;
        }
//        if (!empty($data['to_wallet_id'])) {
//            $where['a.to_wallet_id'] = $data['to_wallet_id'];
//        }



        $where['a.status'] =1;
        $where['a.type']=13;


        $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->count();
        $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $ke=$ke?$ke:0;
        return ['count'=>$count,'list'=>$list,'ke'=>$ke];

    }


    /**
     * 导出购买课程列表
     */
    public function exportCourseList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        set_time_limit(0);

        $where = $where1=[] ;
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {

            $where['c.username'] =$data['search_data'] ;
        }


        $where['a.status'] =1;
        $where['a.type']=13;

        $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();
        foreach ($list as $k=>$v){
            $list[$k]['k']=$k+1;
            $list[$k]['title']=$v->course['name'];
            $list[$k]['add_time']=date('Y-m-d H:i:s',$v->create_time);

        }

//        dd(json_decode(json_encode($list),true));
        $titles = "序号,ID,用户账号,金额（RC）,课程标题,购买时间";
        $keys = "k,id,username,number,title,add_time";

        action_log('导出', '导出购买课程列表');

        export_excel($titles, $keys, $list, '购买课程');
    }


    /**
     * 打赏记录页面
     */
    public function reward_log($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1=[] ;
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {

            $where['c.username'] =$data['search_data'] ;
        }
//        if (!empty($data['to_wallet_id'])) {
//            $where['a.to_wallet_id'] = $data['to_wallet_id'];
//        }



        $where['a.status'] =1;
        $where['a.type']=14;


        $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
        $count =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->count();
        $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $ke=$ke?$ke:0;
        return ['count'=>$count,'list'=>$list,'ke'=>$ke];

    }


    /**
     * 导出打赏记录列表
     */
    public function exportRewardList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        set_time_limit(0);

        $where = $where1=[] ;
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {

            $where['c.username'] =$data['search_data'] ;
        }


        $where['a.status'] =1;
        $where['a.type']=14;

        $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();
        foreach ($list as $k=>$v){
            $list[$k]['k']=$k+1;
            $list[$k]['title']=$v->collection['name'];
            $list[$k]['add_time']=date('Y-m-d H:i:s',$v->create_time);

        }

//        dd(json_decode(json_encode($list),true));
        $titles = "序号,ID,用户账号,金额（RC）,课程内容标题,打赏时间";
        $keys = "k,id,username,number,title,add_time";

        action_log('导出', '导出打赏记录列表');

        export_excel($titles, $keys, $list, '打赏记录');
    }









}
